* *** Synthetic Controls for Outliers
* *** Data setup

* *******************************************************************************************

* **** All outlier payments as a time series among 1% data
* * * Grab the medpar outlier data 

* cd /disk/aging/medicare/data/100pct/med

* foreach YEAR of num 1999/2016{
*  	disp `YEAR'

*  	use `YEAR'/1/med`YEAR', clear

* 	** Keep all outlier payments
* 	keep if outlr_cd == "2"

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/outliers`YEAR'.dta 
* }
* clear

* *** Clean into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
* use outliers1999.dta
* foreach YEAR of numlist 2000/2016{
* 	append using outliers`YEAR'.dta
* }
* gen dt = mofd(dschrgdt)

* collapse (sum) outlramt, by(dt)
* format dt %tm

* twoway scatter outlramt dt
* gen group = "outliers"
* save outliers_panel.dta, replace 

* *******************************************************************************************
* *** Grab DSH payments

* cd /disk/aging/medicare/data/100pct/med

* foreach YEAR of num 1999/2016{
*  	disp `YEAR'

*  	use `YEAR'/1/med`YEAR', clear

* 	** Keep all DSH payments
* 	keep if disp_shr > 0 
* 	keep dschrgdt disp_shr

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/dsh`YEAR'.dta 
* }
* clear

* *** Clean into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
* use dsh1999.dta
* foreach YEAR of numlist 2000/2016{
* 	append using dsh`YEAR'.dta
* }
* gen dt = mofd(dschrgdt)

* collapse (sum) disp_shr, by(dt)
* format dt %tm

* twoway scatter disp_shr dt
* gen group = "dsh"
* save dsh_panel.dta, replace


*******************************************************************************************
*** Grab DME Payments

* cd /disk/aging/medicare/data/100pct/dme

* foreach YEAR of num 1999/2001{
*  	disp `YEAR'

*  	use `YEAR'/dmec`YEAR', clear
*  	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/dme`YEAR'.dta 
* }

* foreach YEAR of num 2002/2005{
*  	disp `YEAR'

*  	use `YEAR'/dmec`YEAR', clear
*  	keep sthrudt pmt_amt
*  	rename sthrudt thru_dt 

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/dme`YEAR'.dta 
* }

* foreach YEAR of num 2006/2016{
*  	disp `YEAR'

*  	use `YEAR'/dmec`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/dme`YEAR'.dta 
* }

* clear

* *** Clean into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
* use dme1999.dta
* foreach YEAR of numlist 2000/2016{
* 	append using dme`YEAR'.dta
* }
* ** >1B obs, gotta try to shrink it
* drop if mi(pmt_amt)
* drop if pmt_amt == 0

* gen dt = mofd(thru_dt)
* drop thru_dt

* collapse (sum) pmt_amt, by(dt)
* format dt %tm

* *twoway scatter pmt_amt dt
* gen group = "dme"
* save dme_panel.dta, replace


*******************************************************************************************
*** SNF data
* cd /disk/aging/medicare/data/100pct/snf

* foreach YEAR of num 1999/2001{
*  	disp `YEAR'

*  	use `YEAR'/snfc`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/snf`YEAR'.dta 
* }

* foreach YEAR of num 2002/2005{
*  	disp `YEAR'

*  	use `YEAR'/snfc`YEAR', clear
*   	keep sthrudt pmt_amt
*   	rename sthrudt thru_dt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/snf`YEAR'.dta 
* }


* foreach YEAR of num 2006/2016{
*  	disp `YEAR'

*  	use `YEAR'/snfc`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/snf`YEAR'.dta 
* }



* *** Clean into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
* use snf1999.dta
* foreach YEAR of numlist 2000/2016{
* 	append using snf`YEAR'.dta
* }
* gen dt = mofd(thru_dt)

* collapse (sum) pmt_amt, by(dt)
* format dt %tm

* twoway scatter pmt_amt dt
* gen group = "snf"
* save snf_panel.dta


*******************************************************************************************
*** HHA data

* cd /disk/aging/medicare/data/100pct/hha

* foreach YEAR of num 1999/2000{
*  	disp `YEAR'

*  	use `YEAR'/hhac`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hha`YEAR'.dta 
* }

* 	** 2001: different filename
* foreach YEAR of num 2001/2001{
*  	disp `YEAR'

*  	use `YEAR'/hha`YEAR', clear
*   	keep thru_dt revpmt1
*   	rename revpmt1 pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hha`YEAR'.dta 
* }


* foreach YEAR of num 2002/2005{
*  	disp `YEAR'

*  	use `YEAR'/hhac`YEAR', clear
*   	keep sthrudt pmt_amt
*   	rename sthrudt thru_dt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hha`YEAR'.dta 
* }

* foreach YEAR of num 2006/2014{
*  	disp `YEAR'

*  	use `YEAR'/hhac`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hha`YEAR'.dta, replace
* }

* foreach YEAR of num 2015{
*  	disp `YEAR'

*  	use `YEAR'/hhac`YEAR', clear
*   	keep clm_thru_dt clm_pmt_amt
*   	rename clm_thru_dt thru_dt
*   	rename clm_pmt_amt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hha`YEAR'.dta, replace
* }

* foreach YEAR of num 2016/2016{
*  	disp `YEAR'

*  	use `YEAR'/hhac`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hha`YEAR'.dta, replace
* }


* *** Clean into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
* use hha1999.dta
* foreach YEAR of numlist 2000/2016{
* 	append using hha`YEAR'.dta
* }
* gen dt = mofd(thru_dt)

* collapse (sum) pmt_amt, by(dt)
* format dt %tm

* twoway scatter pmt_amt dt
* gen group = "hha"
* save hha_panel.dta


*******************************************************************************************
*** Hos data

* cd /disk/aging/medicare/data/100pct/hos
* foreach YEAR of num 1999/2001{
*  	disp `YEAR'

*  	use `YEAR'/hosc`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hos`YEAR'.dta, replace
* }

* foreach YEAR of num 2002/2005{
*  	disp `YEAR'

*  	use `YEAR'/hosc`YEAR', clear
*   	keep sthrudt pmt_amt
*   	rename sthrudt thru_dt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hos`YEAR'.dta, replace
* }

* foreach YEAR of num 2006/2016{
*  	disp `YEAR'

*  	use `YEAR'/hosc`YEAR', clear
*   	keep thru_dt pmt_amt

* 	* Save in my folder
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/hos`YEAR'.dta, replace
* }

*** Clean into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
* use hos1999.dta
* foreach YEAR of numlist 2000/2016{
* 	append using hos`YEAR'.dta
* }
* gen dt = mofd(thru_dt)

* collapse (sum) pmt_amt, by(dt)
* format dt %tm

* twoway scatter pmt_amt dt
* gen group = "hos"
* save hos_panel.dta

*******************************************************************************************
**** Make a single panel
cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
use outliers_panel.dta
append using dsh_panel.dta
append using dme_panel.dta
append using snf_panel.dta
append using hha_panel.dta
append using hos_panel.dta 

gen out = outlramt
replace out = disp_shr if mi(out)
replace out = pmt_amt if mi(out)

*** Clean for use in time shift 
drop disp_shr pmt_amt outlramt

encode group, gen(g)
replace g = 999 if group == "outliers"

drop group
rename g group
rename out pmt_amt


save types_panel.dta, replace





